<?php

if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Skill extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->require_login();
		$this->load->model('skill_model', 'skill');
		$this->load->model('agent_model', 'agent');
	}

	function index() 
	{
		check_permission("skill", "view");
		
		$data = array();
		$data['page_heading'] = 'Skills';
		$GET = $this->input->get(NULL, true);
 		$this->config->set_item('enable_query_strings', TRUE);
		$config['base_url'] = base_url().$this->uri->segment(1).'/?';
		$config['per_page'] = '20';
 		$config['num_links'] = 5;	
		$start = $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$results = $this->skill->getListingData($GET,$start);
		$data['start'] = $start;
		$data['results'] = $results['results'];
		$data['total_record'] = $results['total_record'];
		
		if(is_array(@$GET))	{
			unset($GET['per_page']);
 			$qs = http_build_query($GET); 
		} else {
			$qs = '';	
		}
		
		if(isset($qs) && !empty($qs)){
		   $this->pagination->suffix = "&".$qs;	
		}
		
		$config['total_rows'] = $data['total_record'];
		$config['full_tag_open'] = '<ul class="paging">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		
		$perPage = $config['per_page'];
		$this->config->set_item('enable_query_strings', TRUE);
		$this->pagination->initialize($config);
	
		
		$this->load->view($this->uri->segment(1).'/listing', $data);
	}
	
	function priority($name = '')
	{
		check_permission("skill", "add,edit");
		
		$error_arr = array();
		$penalty_arr = $this->input->post('penalty');
 		if( $this->input->post('sbmt') == 1 ) 
		{
			$less_than_zero = false;
			if(count($penalty_arr) > 0) {
				foreach( $penalty_arr as $id=>$penalty) 
				{
					if( intval($penalty) < 1 ){
						$less_than_zero = true;
					}
				}
			}
			
			if( $less_than_zero ) {
				$error_arr[] = 'Priority must be greater than zero.';	
			}
			
			if( count($error_arr) == 0 )
			{			
 				$this->skill->save_agent_priority($penalty_arr);	
				$this->session->set_flashdata('userMesg', 'Record(s) has been updated successfully.');
				redirect($this->uri->segment(1).'/priority/'.$name);
			}
		}
		
		$data = array();
		$data['error_arr'] = $error_arr;
		$data['page_heading'] = 'Agent priority for '.$name;
		$data['name'] = $name;
		$data['results'] = $this->skill->getSkillMembers($name);
		$this->load->view($this->uri->segment(1).'/priority', $data);
	}
	
	function assign_agent($name = '')
	{
		check_permission("skill", "add,edit");
		
		$agents = $this->input->post('agents');
 		if( $this->input->post('sbmt') == 1 ) {
 			$this->skill->add_agents_to_skill($agents, $name);	
			$this->session->set_flashdata('userMesg', 'Record(s) has been updated successfully.');
			redirect($this->uri->segment(1).'/assign_agent/'.$name);
		}
		
		$data = array();
		$data['page_heading'] = 'Assign agent for '.$name;
		$data['name'] = $name;
		$skill_members = $this->skill->getSkillMembers($name);
		$skill_member_arr = array();
		foreach($skill_members as $row) {
			$skill_member_arr[] = $row->agent;
		}
		$data['agents'] = getAllagent();
		$data['skill_member_arr'] = $skill_member_arr;
		$this->load->view($this->uri->segment(1).'/assign_agent', $data);
	}

	function save($name = '') 
	{
		$name = trim($name);
		$data = array();
		
		if ( $name != '') {
			check_permission("skill", "edit");
		} else {
			check_permission("skill", "add");
		}
		
		$skill_members = $this->skill->getSkillMembers($name);
		$skill_member_arr = array();
		foreach($skill_members as $row) {
			$skill_member_arr[] = $row->agent;
		}
		$data['agents'] = getAllagent();
		$data['skill_member_arr'] = $skill_member_arr;
		
		
		$data['page_heading'] = 'Skills';
		$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|alpha_numeric|callback_check_duplicate['.$name.']');
		$this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean');
		$this->form_validation->set_rules('musiconhold', 'MOH Class', 'trim|required|xss_clean');
		$this->form_validation->set_rules('context', 'Context', 'trim|required|xss_clean');
		$this->form_validation->set_rules('timeout', 'Timeout', 'trim|required|integer|greater_than[-1]|less_than[51]|xss_clean');
		$this->form_validation->set_rules('wrapuptime', 'Warm-up Time', 'trim|required|integer|greater_than[-1]|less_than[51]|xss_clean');
		$this->form_validation->set_rules('strategy', 'Call Routing Strategy', 'trim|required|xss_clean');
		$this->form_validation->set_rules('advancerouting', 'Advance Routing', 'trim|required|xss_clean');
		$this->form_validation->set_rules('server', 'Server', 'trim|required|xss_clean');
		$this->form_validation->set_rules('retry', 'Retry', 'trim|integer|xss_clean');
		$this->form_validation->set_rules('servicelevel', 'Service Level', 'trim|integer|xss_clean');
		
		if ($this->form_validation->run() == FALSE)
		{
			if ( $name != '')
			{
				$detail = $this->skill->getDetailByID( $name );
				foreach($detail as $k=>$v):
				$data[$k] = $v;
				endforeach;
				$data['name'] = $name;
			}
		
			$this->load->view($this->uri->segment(1).'/save', $data);
		} 
		else 
		{
			$record = array( 
							'name' 				=> addslashes(trim($this->common_model->replace_spaces($this->input->post('name')))),
							'description' 		=> addslashes($this->input->post('description')),
							'musiconhold' 		=> addslashes($this->input->post('musiconhold')),
							'context' 			=> addslashes($this->input->post('context')),
							'timeout' 			=> addslashes($this->input->post('timeout')),
							'wrapuptime' 		=> addslashes($this->input->post('wrapuptime')),
							'strategy' 			=> addslashes($this->input->post('strategy')),
							'advancerouting' 	=> addslashes($this->input->post('advancerouting')),
							'server' 			=> addslashes($this->input->post('server')),
							'retry' 			=> addslashes($this->input->post('retry')),
							'servicelevel' 		=> addslashes($this->input->post('servicelevel'))
							);	
							
			if( $name != '' ) {
				$this->skill->update($record);
			} else {
				$this->skill->add($record);
			}
			// saving agent assignment
			$agents = $this->input->post('agents');
			$this->skill->add_agents_to_skill($agents, $record['name']);	
			
			$this->session->set_flashdata('userMesg', 'Record has been updated successfully.');
			redirect($this->uri->segment(1).'/');
		}
	}// end save
  
 	function check_duplicate($name, $old_name = '')
    {
		$name 		= $this->common_model->replace_spaces($name);
		$old_name 	= $this->common_model->replace_spaces($old_name);
		
		$old_name = trim($old_name);
		$where = " name  = '".mysql_real_escape_string($name)."' ";
		if( $old_name != '' && $name == $old_name) {
			return true;
		}
 		if(! $this->common_model->record_exist("queue_table", $where))
		{
			return true;	
		}
		else
		{
			$this->form_validation->set_message('check_duplicate', 'The %s Must be Unique.');
			return false;
		}
    }// end check_duplicate

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */